package cn.edu.laud.sany.datatest;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.sql.SQLException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;

import oracle.net.aso.l;

import cn.edu.laud.sany.types.ImportProcess;
import cn.edu.laud.sany.types.RowData;
import cn.edu.laud.sany.types.TimeSerial;
import cn.edu.laud.sany.utils.Config;
import cn.edu.laud.sany.utils.MyLog;
import cn.edu.laud.sany.utils.ProcessManager;

public class ScanDataResult
{
	public static Integer[] cFS;
	public static String cfsFile = "";
	public static String result1File = "";
	
	public static long allCount = 0;
	
	public static HashMap<Integer, HashMap<String, Long>> allDataCount;
	
	public static BufferedWriter writer1;
	
	/**
	 * @param args
	 * @throws ClassNotFoundException
	 * @throws SQLException
	 */
	public static void main(String[] args) throws ClassNotFoundException, SQLException, IOException
	{
		doMain(args);
	}
	
	protected static void doMain(String[] args) throws ClassNotFoundException, SQLException,
			IOException
	{
		if (args.length != 2)
		{
			System.err.println("parameter error, usage:");
			System.err
					.println("\t java -jar test.jar result1Filename cfFileName");			
			return;
		}
		
		result1File = args[0];
		cfsFile = args[1];
		
		try
		{
			writer1 = new BufferedWriter(new FileWriter(new File("result0.txt")));
		}
		catch (IOException e2)
		{
			e2.printStackTrace();
		}
		allDataCount = new HashMap<Integer, HashMap<String, Long>>();
		
		if (testCfFile() == false)
		{
			System.err.println("can not find CFs file");
			return;
		}
		if (getCFNames() == false)
		{
			System.err.println("can not read CFs correctly");
			return;
		}
		
		long starttime = System.nanoTime();
		
		BufferedReader bReader = new BufferedReader(new FileReader(new File(result1File)));
		String lineData = null;
		while ((lineData = bReader.readLine()) != null)
		{
			if (lineData.isEmpty())
			{
				continue;
			}
			String[] data1s = lineData.split("=");
			String[] data2s = data1s[0].split(":");
			String equipString = data2s[0];
			int paraId = Integer.parseInt(data2s[1]);
			long count = Long.parseLong(data1s[1]);
			if (allDataCount.containsKey(paraId) == false)
			{
				allDataCount.put(paraId, new HashMap<String, Long>());
			}
			if (allDataCount.get(paraId).containsKey(equipString))
			{
				count += allDataCount.get(paraId).get(equipString);
			}
			allDataCount.get(paraId).put(equipString, count);
			allCount += count;
		}
		
		bReader.close();
		
		long endTime = System.nanoTime();
		
		try
		{
			printCountTable();
			writer1.flush();
			writer1.close();
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}
		
		endTime = System.nanoTime();
		System.out.println("All count = " + allCount);
		System.out.println("Program's run time:"
				+ cn.edu.laud.sany.utils.TimeUtils.nanoSecondToString((endTime - starttime)));
	}
	
	protected static void printCountTable() throws IOException
	{
		for (int paraId : cFS)
		{
			HashMap<String, Long> nowHashMap = allDataCount.get(paraId);
			
			long count = 0;
			
			Iterator<Long> iter = nowHashMap.values().iterator();
			while (iter.hasNext())
			{
				long num = iter.next();
				count += num;
			}
			
			writer1.write(String.valueOf(paraId));
			writer1.write("=");
			writer1.write(String.valueOf(count));
			writer1.write("\r\n");
			writer1.flush();
		}
		writer1.write("All count = " + allCount);
	}
	
	protected static boolean getCFNames()
	{
		boolean flag = true;
		File file = new File(cfsFile);
		BufferedReader bReader = null;
		String paraIdString = null;
		ArrayList<Integer> cfList = new ArrayList<Integer>();
		try
		{
			bReader = new BufferedReader(new FileReader(file));
			while ((paraIdString = bReader.readLine()) != null)
			{
				paraIdString = paraIdString.trim();
				if (paraIdString.equals(""))
				{
					continue;
				}
				int paraId = Integer.valueOf(paraIdString);
				cfList.add(paraId);
				allDataCount.put(paraId, new HashMap<String, Long>());
			}
			
		}
		catch (IOException e1)
		{
			flag = false;
			System.out.println("can not open cf file");
		}
		catch (NumberFormatException e2)
		{
			flag = false;
			System.out.println("cf file contains invalid integer");
		}
		finally
		{
			if (flag == false)
			{
				System.out.println("get cfs failed!");
			}
			try
			{
				if (bReader != null)
				{
					bReader.close();
				}
			}
			catch (IOException e)
			{
				System.out.println(e.getMessage());
			}
		}
		if (flag == true)
		{
			cFS = cfList.toArray(new Integer[0]);
		}
		return flag;
	}
	
	protected static boolean testCfFile()
	{
		try
		{
			File file = new File(cfsFile);
			if (file.exists())
			{
				return true;
			}
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		return false;
	}
	
}
